﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:APP_SPECIALINFO
	/// </summary>
	public partial class APP_SPECIALINFO : BaseDAL<GSLMS.Entity.TB_APP_SPECIALINFO>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public APP_SPECIALINFO()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_APP_SPECIALINFO model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_REGINFOID", model.REGINFOID),
								new OracleParameter("P_CUSTNAME", model.CUSTNAME),
								new OracleParameter("P_SALESMANID", model.SALESMANID),
								new OracleParameter("P_SALESMAN", model.SALESMAN),
								new OracleParameter("P_DEPID", model.DEPID),
								new OracleParameter("P_DEPNAME", model.DEPNAME),
								new OracleParameter("P_PRODUCTID", model.PRODUCTID),
								new OracleParameter("P_PRODUCTNAME", model.PRODUCTNAME),
								new OracleParameter("P_SPECIALTITLE", model.SPECIALTITLE),
								new OracleParameter("P_SPECIALTYPE", model.SPECIALTYPE),
								new OracleParameter("P_DETAIL", model.DETAIL),
								new OracleParameter("P_STATUS", model.STATUS),
								new OracleParameter("P_CREATEBY", model.CREATEBY),
								new OracleParameter("P_CREATEDATE", model.CREATEDATE),
								new OracleParameter("P_SPAREFIELD1", model.SPAREFIELD1),
								new OracleParameter("P_SPAREFIELD2", model.SPAREFIELD2),
								new OracleParameter("P_SPAREFIELD3", model.SPAREFIELD3)};
  
			for (int i = 0; i < 18; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_specialinfo_add", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_APP_SPECIALINFO model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_REGINFOID", model.REGINFOID),
								new OracleParameter("P_CUSTNAME", model.CUSTNAME),
								new OracleParameter("P_SALESMANID", model.SALESMANID),
								new OracleParameter("P_SALESMAN", model.SALESMAN),
								new OracleParameter("P_DEPID", model.DEPID),
								new OracleParameter("P_DEPNAME", model.DEPNAME),
								new OracleParameter("P_PRODUCTID", model.PRODUCTID),
								new OracleParameter("P_PRODUCTNAME", model.PRODUCTNAME),
								new OracleParameter("P_SPECIALTITLE", model.SPECIALTITLE),
								new OracleParameter("P_SPECIALTYPE", model.SPECIALTYPE),
								new OracleParameter("P_DETAIL", model.DETAIL),
								new OracleParameter("P_STATUS", model.STATUS),
								new OracleParameter("P_CREATEBY", model.CREATEBY),
								new OracleParameter("P_CREATEDATE", model.CREATEDATE),
								new OracleParameter("P_SPAREFIELD1", model.SPAREFIELD1),
								new OracleParameter("P_SPAREFIELD2", model.SPAREFIELD2),
								new OracleParameter("P_SPAREFIELD3", model.SPAREFIELD3)};
  
			for (int i = 0; i < 18; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_specialinfo_update", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", ID)};
  
				parms[0].Direction = ParameterDirection.Input;

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_specialinfo_delete", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_APP_SPECIALINFO DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_APP_SPECIALINFO model = new GSLMS.Entity.TB_APP_SPECIALINFO();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["REGINFOID"]!=null && dr["REGINFOID"].ToString()!="")
				{
					model.REGINFOID=OracleHelper.ToLong(dr["REGINFOID"].ToString());
				}
				if(dr["CUSTNAME"]!=null && dr["CUSTNAME"].ToString()!="")
				{
					model.CUSTNAME=dr["CUSTNAME"].ToString();
				}
				if(dr["SALESMANID"]!=null && dr["SALESMANID"].ToString()!="")
				{
					model.SALESMANID=OracleHelper.ToLong(dr["SALESMANID"].ToString());
				}
				if(dr["SALESMAN"]!=null && dr["SALESMAN"].ToString()!="")
				{
					model.SALESMAN=dr["SALESMAN"].ToString();
				}
				if(dr["DEPID"]!=null && dr["DEPID"].ToString()!="")
				{
					model.DEPID=OracleHelper.ToLong(dr["DEPID"].ToString());
				}
				if(dr["DEPNAME"]!=null && dr["DEPNAME"].ToString()!="")
				{
					model.DEPNAME=dr["DEPNAME"].ToString();
				}
				if(dr["PRODUCTID"]!=null && dr["PRODUCTID"].ToString()!="")
				{
					model.PRODUCTID=OracleHelper.ToLong(dr["PRODUCTID"].ToString());
				}
				if(dr["PRODUCTNAME"]!=null && dr["PRODUCTNAME"].ToString()!="")
				{
					model.PRODUCTNAME=dr["PRODUCTNAME"].ToString();
				}
				if(dr["SPECIALTITLE"]!=null && dr["SPECIALTITLE"].ToString()!="")
				{
					model.SPECIALTITLE=dr["SPECIALTITLE"].ToString();
				}
				if(dr["SPECIALTYPE"]!=null && dr["SPECIALTYPE"].ToString()!="")
				{
					model.SPECIALTYPE=dr["SPECIALTYPE"].ToString();
				}
				if(dr["DETAIL"]!=null && dr["DETAIL"].ToString()!="")
				{
					model.DETAIL=dr["DETAIL"].ToString();
				}
				if(dr["STATUS"]!=null && dr["STATUS"].ToString()!="")
				{
                    model.STATUS = OracleHelper.ToInt(dr["STATUS"].ToString());
				}
				if(dr["CREATEBY"]!=null && dr["CREATEBY"].ToString()!="")
				{
					model.CREATEBY=dr["CREATEBY"].ToString();
				}
				if(dr["CREATEDATE"]!=null && dr["CREATEDATE"].ToString()!="")
				{
					model.CREATEDATE=DateTime.Parse(dr["CREATEDATE"].ToString());
				}
				if(dr["SPAREFIELD1"]!=null && dr["SPAREFIELD1"].ToString()!="")
				{
                    model.SPAREFIELD1 = OracleHelper.ToInt(dr["SPAREFIELD1"].ToString());
				}
				if(dr["SPAREFIELD2"]!=null && dr["SPAREFIELD2"].ToString()!="")
				{
					model.SPAREFIELD2=dr["SPAREFIELD2"].ToString();
				}
				if(dr["SPAREFIELD3"]!=null && dr["SPAREFIELD3"].ToString()!="")
				{
					model.SPAREFIELD3=dr["SPAREFIELD3"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_APP_SPECIALINFO GetByID(long ID)
		{
			GSLMS.Entity.TB_APP_SPECIALINFO sModel = new GSLMS.Entity.TB_APP_SPECIALINFO();
            ADU_SPECAIL aduDal = new ADU_SPECAIL();
            DataTable aduDt = null;


            OracleParameter[] parms = new OracleParameter[] { 
              new OracleParameter("p_id",ID),
              new OracleParameter("p_cur",OracleType.Cursor),
              new OracleParameter("p_aducur",OracleType.Cursor)
            };

            parms[0].Direction = ParameterDirection.Input;
            parms[1].Direction = ParameterDirection.Output;
            parms[2].Direction = ParameterDirection.Output;

            DataSet ds = OracleHelper.ExecuteDataset(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_specialinfo_get", parms);


            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                sModel = DataRowToModel(ds.Tables[0].Rows[0]);

                if (ds.Tables.Count > 1)
                    aduDt = ds.Tables[1];

                if (aduDt != null)
                {
                    var aduList = new List<GSLMS.Entity.TB_ADU_SPECAIL>();

                    for (int i = 0; i < aduDt.Rows.Count; i++)
                    {
                        GSLMS.Entity.TB_ADU_SPECAIL model = new GSLMS.Entity.TB_ADU_SPECAIL();
                        model = aduDal.DataRowToModel(aduDt.Rows[i]);
                        aduList.Add(model);
                    }

                    sModel.AduList = aduList;
                }
            }
			else
			{
			return null;
			}
			return sModel;
		}

		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_APP_SPECIALINFO> GetList(GSLMS.Entity.TB_APP_SPECIALINFO searchParam)
		{
			var list=new List<GSLMS.Entity.TB_APP_SPECIALINFO>();
			DataTable dt = null;
			string tblName = "TB_APP_SPECIALINFO";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
			string strFields="*";
			int totalCount = 0;
			int countFlag = 1;

            //是否计算总数
            countFlag = (searchParam.Pagination.ReCount != null && searchParam.Pagination.ReCount) ? 1 : 0;
            pageSize = (searchParam.Pagination.PageSize != null && searchParam.Pagination.PageSize > 0) ? searchParam.Pagination.PageSize : 15;
            pageIndex = (searchParam.Pagination.CurrentIndex != null && searchParam.Pagination.CurrentIndex > 0) ? searchParam.Pagination.CurrentIndex : 1;
            strSort = searchParam.StrSort;

            //查询条件 客户名称 申请日期 至   类型 状态

            if (searchParam.CUSTNAME != null && searchParam.CUSTNAME.Trim() != "")
            {
                sqlWhere += " and custname like '%" + OracleHelper.ReplaceUnsafeSql(searchParam.CUSTNAME) + "%'";
            }

            if (searchParam.StarDateTime != null && searchParam.StarDateTime.ToString().Trim() != "")
            {
                sqlWhere += " and createdate>=to_date('" + Convert.ToDateTime(searchParam.StarDateTime).ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss')";
            }

            if (searchParam.EndDateTime != null && searchParam.EndDateTime.ToString().Trim() != "")
            {
                sqlWhere += " and createdate<to_date('" + Convert.ToDateTime(searchParam.EndDateTime).AddDays(1).AddSeconds(-1).ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss')";
            }

            if (searchParam.SPECIALTITLE != null && searchParam.SPECIALTYPE.Trim() != "")
            {
                sqlWhere += " and SPECIALTYPE='"+searchParam.SPECIALTYPE.Trim()+"'";
            }


            if (searchParam.STATUS != null && searchParam.STATUS > -1)
            {
                sqlWhere += " and STATUS=" + searchParam.STATUS.ToString();
            }


			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
            if (searchParam.Pagination.ReCount)
            {
                searchParam.RowCount = totalCount;
                searchParam.Pagination.RowCount = totalCount;
            }

			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_APP_SPECIALINFO model = new GSLMS.Entity.TB_APP_SPECIALINFO();
					model = DataRowToModel(dt.Rows[i]);
                  
					list.Add(model);
				}
			}
			else
			{
				return null;
			}
			return list;
		}

		#endregion  Method
	}
}

